package com.ts.proj.json;

import com.alibaba.fastjson.JSONArray;
import com.ts.base.controller.BaseController;
import com.ts.common.constant.InsUpdDelFlagConstant;
import com.ts.proj.entity.SystemFieldEntity;
import com.ts.proj.service.SystemFieldService;
import com.ts.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class SystemFieldJson extends BaseController {
    @Autowired
    private SystemFieldService systemFieldService;

    /**
     * 显示list
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/field-list-json/{page}/{size}")
    public Map<String,Object> localConstantList(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        PageData pd = this.getPageData();
        PageData pageData = new PageData();
        pageData.put("deleteFlag", "N");
        if (null!=pd.get("tableName")&&!"".equals(pd.getString("tableName"))) {
            pageData.put("tableName", pd.getString("tableName"));
        }
        long count = systemFieldService.count(pageData);
        List<SystemFieldEntity> systemConstantEntities = systemFieldService.findAll(pageData,page-1,size);
        pageData.put("total",count);
        pageData.put("rows", systemConstantEntities);
        return pageData;
    }

    @RequestMapping(value = "/field-ins-upd-del/{id}")
    public Object fieldAdd(@PathVariable("id") String id) {
        PageData pd = this.getPageData();
        try{
            SystemFieldEntity byId = systemFieldService.findById(Integer.valueOf(id));
            if(null != pd.get("fieldCn")&&!"".equals(pd.getString("fieldCn"))){
                byId.setFieldCn(pd.getString("fieldCn"));
            }
            systemFieldService.save(byId);
        }catch (Exception e){

        }
        return "success";
    }

    @RequestMapping("/field-search-list")
    public Object fieldSearchList(){
        List<PageData> pageDataList = new ArrayList<PageData>();
        PageData tempPa = new PageData();
        tempPa.put("deleteFlag", InsUpdDelFlagConstant.DELETE_FLAG_N);
        List<String> systemFieldEntities = systemFieldService.findFieldTable();
        for(String str : systemFieldEntities){
            PageData pd = new PageData();
            pd.put("value",str);
            pd.put("text",str);
            pageDataList.add(pd);
        }
        return JSONArray.toJSONString(pageDataList);
    }

}
